<?php

namespace app\controller;

use app\model\AdminLoginRecord;
use app\model\AdminModel;
use app\model\LoginRecord;
use app\model\UserModel;
use app\Request;
use Firebase\JWT\JWT;

class Login extends Base
{

    //测试
    public function index()
    {
        return 111111;

    }

    //用户登录接口
    public function userLogin(Request $request)
    {
        $json = array();
        //获取前端传送过来的数据
        $phone = $request->param('accountNumber');
        $pwd = $request->param('pwd');
        //数据表验证
        $user = new UserModel();
        $info = $user->where('phone', $phone)->find();
//        echo md5($pwd);
        if (!$info) {
            return json(['code' => 0, 'msg' => '账号不存在']);
        }
        if ($info['password'] != md5($pwd)) {
            return json(['code' => 0, 'msg' => '账号或密码错误']);
        }

        $ifcheck=$user->where('phone', $phone)->value('ifcheck');
        $username=$user->where('phone', $phone)->value('user_name');

//      jwt json web token  保存登录状态
        $jwt = new JWT();
        $key = 'api123456';
        $payload = [
            'iss' => 'http://www.caihuay.com.cn',
            'aud' => 'http://www.caihuay.com.cn',
            'iat' => time(),
            'nbf' => time(),
            'aid' => $info['user_id']
        ];
        $token = $jwt::encode($payload, $key, 'HS256');

        // 插入登录记录
        $ip = request()->ip(); // 获取ip
        $record=[
            'user_id' => $info['user_id'],
            'user_login_ip' => $ip,
            'user_token' => $token
        ];

        LoginRecord::create($record);

        if($ifcheck==1) //已审核通过
        {
            return json(['code' => 1, 'msg' => '登录成功', 'token' => $token,'username'=>$username,'check'=>$ifcheck]);
        }
        else{   //审核不通过 和 未审核
            return json(['code' => -1, 'msg' => '登录成功，请及时完成身份认证！', 'token' => $token,'username'=>$username,'check'=>$ifcheck]);
        }
    }

    // 用户注册接口
    public function register(Request $request)
    {
        $json = array();
        $phone = $request->param('phone');
        $password = $request->param('pwd');
        $username=$request->param('username');
        $identity=$request->param('identity');
        if (UserModel::where('phone',$phone)->find() == null) {
            if(UserModel::where('user_name',$username)->find() == null)
            {
                UserModel::create([
                    'user_name' => $username,
                    'phone' => $phone,
                    'password' => md5($password),
                    'identity' => $identity,
                    'img'=>'default_user_img.jpg'
                ]);
                $json['result'] = 1;  //注册成功
            }
            else{
                $json['result'] = 0;  //用户名已存在
            }
        } else {
            $json['result'] = -1; // 已经注册过
        }
        return json($json);
    }

//管理员登录接口
    public function adminLogin(Request $request)
    {

        //获取前端传送过来的数据
        $admin_account = $request->param('admin_account');
        $admin_password = $request->param('admin_password');
        //数据表验证
        $admin = new AdminModel();
        $info = $admin->where('admin_account', $admin_account)->find();
//        echo md5($admin_password);

        if (!$info) {
            return json(['code' => 0, 'msg' => '账号不存在']);
        }
        if ($info['admin_password'] != md5($admin_password)) {
            return json(['code' => 0, 'msg' => '账号或密码错误']);
        }


//      jwt json web token  保存登录状态
        $jwt = new JWT();
        $key = 'api123456';
        $payload = [
            'iss' => 'http://www.caihuay.com.cn',
            'aud' => 'http://www.caihuay.com.cn',
            'iat' => time(),
            'nbf' => time(),
            'aid' => $info['admin_id']
        ];
        $token = $jwt::encode($payload, $key, 'HS256');

        // 插入登录记录
        $ip = request()->ip(); // 获取ip
        $record=[
            'admin_id' => $info['admin_id'],
            'admin_login_ip' => $ip,
            'admin_token' => $token
        ];

       AdminLoginRecord::create($record);

       return json(['code' => 1, 'msg' => '登录成功', 'token' => $token,'admin_name'=>$info['admin_name']]);



    }



}
